<?php

namespace app\index\controller;

use tian\Session;

class Package extends Base
{
    public function myEquip()
    {
        try {
            $this->role_id = Session::get('uid');

            //获取用户信息
            $info = model('Role')->getInfo($this->role_id);
            if (!empty($info)) {
                $info['tool1Name'] = $this->getEquipName($info['tool1']);
                $info['tool2Name'] = $this->getEquipName($info['tool2']);
                $info['tool3Name'] = $this->getEquipName($info['tool3']);
                $info['tool4Name'] = $this->getEquipName($info['tool4']);
                $info['tool5Name'] = $this->getEquipName($info['tool5']);
                $info['tool6Name'] = $this->getEquipName($info['tool6']);

                if ($info['usex'] == '女') {
                    $info['usexImg'] = '/public/admin/image/nv.jpg';
                } else {
                    $info['usexImg'] = '/public/admin/image/nan.jpg';
                }
            }
            $this->assign('info', $info);

            $mapEquip = [];
            $mapEquip[] = ['uid', '=', $info['uid']];
            $mapEquip[] = ['status', '=', 1];
            $listMyEquip = model('RoleEquip')->getList($mapEquip);
            if (!empty($listMyEquip)) {
                foreach ($listMyEquip as $item) {
                    $item['zbtoolName'] = model('Equip')->getZbtoolAttr($item['zbtool']);
                    $item['thumb'] = model('Equip')->findValue(['zbid' => $item['zbid']], 'thumb');
                }
            }
            $this->assign('listMyEquip', $listMyEquip);

            $surplusLattice = count($listMyEquip) >= 30 ? 0 : (30 - count($listMyEquip));
            $this->assign('surplusLattice', $surplusLattice + 1);

            $this->fetch('my_equip');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    public function getEquipName($zbid)
    {
        if ($zbid != 0) {
            $zbname = model('RoleEquip')->getWhereValue(['zbnowid' => $zbid], 'zbname');
            return $zbname;
        } else {
            return '';
        }
    }

    public function getRoleEquip()
    {
        $id = input('id');
        $info = model('RoleEquip')->getInfo($id);
        if (!empty($info)) {
            returnJson(1, 'ok', $info);
        } else {
            returnJson(0, '数据不存在');
        }
    }

    public function equipAct()
    {
        try {
            $request = request();
            if ($request->method() == 'POST') {
                $role_id = Session::get('uid');

                $act = $request->post('act');
                $id = $request->post('id');

                $infoRole = model('Role')->getInfo($role_id);
                $infoRoleEquip = model('RoleEquip')->getInfo($id);

                if ($act == 'wear') {
                    $mapRole = [];
                    $mapEquip = [];
                    $map = [];

                    $mapRole['uid'] = $role_id;
                    if ($infoRoleEquip['zbtool'] == 1) {
                        $mapRole['tool1'] = $id;
                        if ($infoRole['tool1'] != 0) {
                            $mapEquip['zbnowid'] = $infoRole['tool1'];
                        }
                    }
                    if ($infoRoleEquip['zbtool'] == 2) {
                        $mapRole['tool2'] = $id;
                        if ($infoRole['tool2'] != 0) {
                            $mapEquip['zbnowid'] = $infoRole['tool2'];
                        }
                    }
                    if ($infoRoleEquip['zbtool'] == 3) {
                        $mapRole['tool3'] = $id;
                        if ($infoRole['tool3'] != 0) {
                            $mapEquip['zbnowid'] = $infoRole['tool3'];
                        }
                    }
                    if ($infoRoleEquip['zbtool'] == 4) {
                        $mapRole['tool4'] = $id;
                        if ($infoRole['tool4'] != 0) {
                            $mapEquip['zbnowid'] = $infoRole['tool4'];
                        }
                    }
                    if ($infoRoleEquip['zbtool'] == 5) {
                        $mapRole['tool5'] = $id;
                        if ($infoRole['tool5'] != 0) {
                            $mapEquip['zbnowid'] = $infoRole['tool5'];
                        }
                    }
                    if ($infoRoleEquip['zbtool'] == 6) {
                        $mapRole['tool6'] = $id;
                        if ($infoRole['tool6'] != 0) {
                            $mapEquip['zbnowid'] = $infoRole['tool6'];
                        }
                    }
                    model('Role')->saved($mapRole);

                    $map['zbnowid'] = $id;
                    $map['status'] = 2;
                    model('RoleEquip')->saved($map);

                    if (!empty($mapEquip)) {
                        $mapEquip['status'] = 1;
                        model('RoleEquip')->saved($mapRole);
                    }
                }

                if ($act == 'sell') {
                    $pay = $request->post('pay', 0);
                    if ($pay == 0) {
                        returnJson(0, '请输入价格');
                    }

                    $map = [];

                    $map['zbnowid'] = $id;
                    $map['status'] = 3;
                    model('RoleEquip')->saved($map);

                    $add = [];
                    $add['pay'] = $pay;
                    $add['zbnowid'] = $id;
                    $add['uid'] = $role_id;
                    $add['zbid'] = $infoRoleEquip['zbid'];
                    $add['zblv'] = $infoRoleEquip['zblv'];
                    $add['zbtool'] = $infoRoleEquip['zbtool'];
                    $add['zbname'] = $infoRoleEquip['zbname'];
                    $add['zbinfo'] = $infoRoleEquip['zbinfo'];
                    $add['zbhp'] = $infoRoleEquip['zbhp'];
                    $add['zbgj'] = $infoRoleEquip['zbgj'];
                    $add['zbfy'] = $infoRoleEquip['zbfy'];
                    $add['zbbj'] = $infoRoleEquip['zbbj'];
                    $add['zbxx'] = $infoRoleEquip['zbxx'];
                    $add['qianghua'] = $infoRoleEquip['qianghua'];
                    model('MarketEquip')->add($add);
                }

                if ($act == 'lose') {
                    model('RoleEquip')->del($id);
                }

                if ($act == 'remove') {
                    $map['zbnowid'] = $id;
                    $map['status'] = 1;
                    model('RoleEquip')->saved($map);

                    if ($infoRoleEquip['zbtool'] == 1) {
                        $mapRole['tool1'] = 0;
                    }
                    if ($infoRoleEquip['zbtool'] == 2) {
                        $mapRole['tool2'] = 2;
                    }
                    if ($infoRoleEquip['zbtool'] == 3) {
                        $mapRole['tool3'] = 0;
                    }
                    if ($infoRoleEquip['zbtool'] == 4) {
                        $mapRole['tool4'] = 0;
                    }
                    if ($infoRoleEquip['zbtool'] == 5) {
                        $mapRole['tool5'] = 0;
                    }
                    if ($infoRoleEquip['zbtool'] == 6) {
                        $mapRole['tool6'] = 0;
                    }
                    $mapRole['uid'] = $role_id;
                    model('Role')->saved($mapRole);
                }

                returnJson(1, 'ok');
            } else {
                returnJson(0, '非法请求');
            }
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    public function myProp()
    {
        try {
            $role_id = Session::get('uid');

            //获取用户信息
            $info = model('Role')->getInfo($role_id);
            $this->assign('info', $info);

            $map = [];
            $map[] = ['uid', '=', $info['uid']];
            $map[] = ['djzl', '=', 2];
            $list = model('RoleProp')->getList($map);
            $this->assign('list', $list);

            $surplusLattice = count($list) >= 30 ? 0 : (30 - count($list));
            $this->assign('surplusLattice', $surplusLattice + 1);

            $this->fetch('my_prop');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    public function propAct()
    {
        try {
            $request = request();
            if ($request->method() == 'POST') {
                $role_id = Session::get('uid');

                $act = $request->post('act');
                $id = $request->post('id');

                $infoRoleProp = model('RoleProp')->getInfo($id);

                if ($infoRoleProp['uid'] != $role_id) {
                    returnJson(0, '非法操作');
                }

                if ($act == 'sell') {
                    $num = $request->post('num', 0);
                    $pay = $request->post('pay', 0);

                    if (gt0($num) == false) {
                        returnJson(0, '请输入数量');
                    }
                    if (gt0($pay) == false) {
                        returnJson(0, '请输入价格');
                    }
                    if ($infoRoleProp['djsum'] < $num) {
                        returnJson(0, '数量不足');
                    }

                    $add = [];
                    $add['pay'] = $pay;
                    $add['uid'] = $role_id;
                    $add['djid'] = $infoRoleProp['djid'];
                    $add['djname'] = $infoRoleProp['djname'];
                    $add['djinfo'] = $infoRoleProp['djinfo'];
                    $add['djcount'] = $infoRoleProp['djsum'];
                    model('MarketProp')->add($add);

                    if ($infoRoleProp['djsum'] == $num) {
                        model('RoleProp')->del($id);
                    } else {
                        $temp = [
                            'id' => $id,
                            'djsum' => $infoRoleProp['djsum'] - $num,
                        ];
                        model('RoleProp')->saved($temp);
                    }
                }

                if ($act == 'lose') {
                    model('RoleProp')->del($id);
                }

                returnJson(1, 'ok');
            } else {
                returnJson(0, '非法请求');
            }
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    public function myDrug()
    {
        try {
            $role_id = Session::get('uid');

            //获取用户信息
            $info = model('Role')->getInfo($role_id);
            $this->assign('info', $info);

            $where = [];
            $where[] = ['uid', '=', $info['uid']];
            $list = model('RoleDrug')->getList($where);
            if (!empty($list)) {
                foreach ($list as $k => $item) {
                    if ($item['ypinfo'] == '' || $item['ypinfo'] == null) {
                        $map['ypid'] = $item['ypid'];
                        $item['ypinfo'] = model('Drug')->findValue($map, 'ypinfo');
                    }
                }
            }
            $this->assign('list', $list);

            $surplusLattice = count($list) >= 30 ? 0 : (30 - count($list));
            $this->assign('surplusLattice', $surplusLattice + 1);

            $this->fetch('my_drug');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    public function drugAct()
    {
        try {
            $request = request();
            if ($request->method() == 'POST') {
                $role_id = Session::get('uid');

                $act = $request->post('act');
                $id = $request->post('id');

                $infoRole = model('Role')->getInfo($role_id);
                $infoRoleDrug = model('RoleDrug')->getInfo($id);
                if (empty($infoRoleDrug) || $infoRoleDrug['uid'] != $role_id) {
                    returnJson(0, '非法操作');
                }

                if ($act == 'lattice') {
                    $yp1 = $infoRole['yp1'];
                    $yp2 = $infoRole['yp2'];

                    $temp = [
                        'uid' => $role_id,
                        'yp1' => $id,
                        'yp2' => $yp1,
                        'yp3' => $yp2,
                    ];
                    model('Role')->saved($temp);
                }

                if ($act == 'eat') {
                    if ($infoRoleDrug['type'] == 1) {
                        $uhp = $infoRole['uhp'] + $infoRoleDrug['yphp'];
                        $ump = $infoRole['ump'] + $infoRoleDrug['ypmana'];
                        if ($uhp > $infoRole['umaxhp']) $uhp = $infoRole['umaxhp'];
                        if ($ump > $infoRole['umaxmp']) $ump = $infoRole['umaxmp'];

                        $temp = [
                            'uid' => $role_id,
                            'uhp' => $uhp,
                            'ump' => $ump,
                        ];
                        model('Role')->saved($temp);
                    }
                    if ($infoRoleDrug['type'] == 2) {
                        $uexp = $infoRole['uexp'] + $infoRoleDrug['exp'];

                        $temp = [
                            'uid' => $role_id,
                            'uexp' => $uexp,
                        ];
                        model('Role')->saved($temp);
                    }
                    if ($infoRoleDrug['type'] == 3) {
                        returnJson(0, '增益类正在加紧开发中');
                    }

                    //减少数量
                    if ($infoRoleDrug['ypsum'] == 1) {
                        model('RoleDrug')->del($id);
                    } else {
                        $tempRoleDrug = [
                            'id' => $id,
                            'ypsum' => $infoRoleDrug['ypsum'] - 1,
                        ];
                        model('RoleDrug')->saved($tempRoleDrug);
                    }
                }

                if ($act == 'lose') {
                    model('RoleDrug')->del($id);
                }

                returnJson(1, 'ok');
            } else {
                returnJson(0, '非法请求');
            }
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    public function myTask()
    {
        try {
            $role_id = Session::get('uid');

            //获取用户信息
            $info = model('Role')->getInfo($role_id);
            $this->assign('info', $info);

            $map = [];
            $map[] = ['uid', '=', $info['uid']];
            $map[] = ['djzl', '=', 1];
            $list = model('RoleProp')->getList($map);
            $this->assign('list', $list);

            $surplusLattice = count($list) >= 30 ? 0 : (30 - count($list));
            $this->assign('surplusLattice', $surplusLattice + 1);

            $this->fetch('my_task');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

}
